package com.haoma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoma.entity.AboutMe;
import com.haoma.entity.Technology;
import com.haoma.exception.BlogException;
import com.haoma.mapper.AboutMeMapper;
import com.haoma.mapper.TechnologyMapper;
import com.haoma.service.AboutMeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Description:
 */
@Service
public class AboutMeServiceImpl extends ServiceImpl<AboutMeMapper, AboutMe> implements AboutMeService {


    @Autowired
    private TechnologyMapper technologyMapper;

    /**
     * 获取关于我的信息
     *
     * @return
     */
    public AboutMe getAboutMe() {
        AboutMe aboutMe = getById(1);
        List<Technology> technologies = technologyMapper.selectList(new LambdaQueryWrapper<>());
        if (!CollectionUtils.isEmpty(technologies)) {
            aboutMe.setTechnology(technologies);
        }
        return aboutMe;
    }

    /**
     * 修改关于我
     *
     * @param aboutMe
     */
    public void updateAboutMe(AboutMe aboutMe) {
        updateById(aboutMe);
    }

    /**
     * 删除技术栈
     *
     * @param id
     */
    public void delTechnology(Integer id) {
        Technology technology = technologyMapper.selectById(id);
        if (technology == null) {
            throw new BlogException("删除失败");
        }
        technologyMapper.deleteById(id);
    }

    /**
     * 添加技术栈
     *
     * @param name
     */
    public void addTechnology(String name) {
        Technology technology = new Technology();
        technology.setName(name);
        technologyMapper.insert(technology);
    }
}
